﻿ @Styles.Render("~/Content/dhtmlxTree/css")
 @Scripts.Render("~/Content/dhtmlxTree/js")
<script>
    var myTree;
    function loadTree() {
        myTree = dhtmlXTreeFromHTML("treeboxbox_tree"); // 加载检索树
        myTree.enableThreeStateCheckboxes(true);

        //设置节点单击事件
        myTree.attachEvent("onClick", function (id) {
            searchSingle(id); //选择事件          
        });

        //设置节点展开(event=1)/合拢(event=-1)结束事件
        myTree.attachEvent("onOpenEnd", function (nodeId, event,e) {
            if (event != -1) {
                //点击加减号触发
                treeClick(nodeId,e);
            }
        });
    }
    function treeClick(id,e) {
        var select = $('#categoryType').val();
        var dbCode = $('#dbCode').val();
        myTree.deleteChildItems(id);//加载之前先删除父节点下面的子结点
        var checkstr = "";
        if (myTree.isItemChecked(id))
        {
            checkstr = "CHECKED";
        }
        var url = "/AddNavi/GetCategroyChilds?Code=" + id + "&type=" + select + "&dbCode=" + dbCode;//发送AJAX的URL
        $.getJSON(
          url,
          function (data) {
              $.each(data, function (index, entry) {//ajax回调后,类似于for循环.
                  //新建子节点，前三个参数为必须的(父节点ID, 新增节点ID, 新增节点名称);
                 //insertNewChild(parentId,itemId,itemText,itemActionHandler,image1,image2,image3,optionStr,childs)   
                  myTree.insertNewChild(id, entry['Code'], entry['Name'], '', '', '', '', checkstr, '');//使用这个函数.
                  if (entry['ChildFlag'] == 1) {
                      myTree.insertNewChild(entry['Code'], entry['Code']+'_c', '', '', '', '', '', checkstr, '');//如果该节点下有子节点，添加空节点【占位符】
                      myTree.closeItem(entry['Code']);
                  }
              });
          }

         );

    }

    $(document).ready(function () {
        loadTree(); //加载检索树
        //checkAll(); //默认全选
    });

    function searchSingle(id) {
        $('#pageIndexNow').val('1');
        var select = $('#categoryType').val();
        $('#selectedCatTypeID').attr("value", select);
        $('#checkedCatID').val(id);
        var searchForm = $('#searchForm');
        searchForm.submit();
    }

    function checkAll() {
        var arrayNodes = myTree.getAllUnchecked();
        if (arrayNodes != "") {
            arrayNodes = arrayNodes.split(',');

            for (var i = 0; i < arrayNodes.length; i++) {
                myTree.setCheck(arrayNodes[i], true);
            }
        }
        //--全部选中
    }
    function unCheckAll() {
        var arrayNodes = myTree.getAllChecked();
        if (arrayNodes != "") {
            arrayNodes = arrayNodes.split(',');

            for (var i = 0; i < arrayNodes.length; i++) {
                myTree.setCheck(arrayNodes[i], false);
            }
        }
        //取消选中
    }
</script>

<div id="treeboxbox_tree" enableCheckBoxes="1" enableTreeImages="0" enableMultiLineItems="1"  enableAutoTooltips="1"
    setImagePath="/Content/dhtmlxTree/codebase/imgs/dhxtree_skyblue/" >
    <ul>
    @{ 
        if (ViewBag.CategoryList != null)
         {
                foreach (var item in ViewBag.CategoryList)
                {
                  @Html.Raw("<li  id="+item.Code+">" + item.Name);

                  string ChildID = item.Code + "_c"; //"_c" 只是一个标识：为了区分子节点和父节点的id ，可以用任意字符替代，但是该节点必须以其根节点的itemCode开头，代表该节点下的第一个节点

                  if (item.ChildFlag == 1)//如果该节点下有子节点，添加【占位符】因为该控件在加载时会判断节点下有无子节点，
                  //有则显示加号，没有则不显示加号，这样就会造成初次加载根节点时不会显示加号，因此先添加占位符，显示加号，点击加号时加载下面的子节点
                  {
                      @Html.Raw("<ul><li id="+ChildID+"> </ul>")             
                  }
                }
         }
      }
</ul>
</div>
